﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Print_Print2h : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["TuNgayPrintPhuLuc2h"] != null && Session["DenNgayPrintPhuLuc2h"] != null)
            {
                try
                {
                    binding();
                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment; filename=Phuluc2h.xls");
                    Response.Charset = "";
                    Response.ContentType = "application/vnd.xls";
                    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                    grid_Detail.RenderControl(htmlWrite);
                    Response.Write(stringWrite.ToString());
                    Response.End();
                    Response.Redirect("javascript:window.close()");
                }
                catch (Exception ex)
                {
                    Response.Write("<script>alert('" + ex.Message + "')</script>");
                }
                Session.Remove("TuNgayPrintPhuLuc2h");
                Session.Remove("DenNgayPrintPhuLuc2h");
            }
        }
    }
    public void binding()
    {
        _dbDataContext _db = new _dbDataContext();
        DateTime tungay_dt = CatalogAccess.ConvertToDate(Session["TuNgayPrintPhuLuc2h"].ToString());
        DateTime toingay_dt = CatalogAccess.ConvertToDate(Session["DenNgayPrintPhuLuc2h"].ToString());
        var listcosodoan = _db.tblCoSoDoans.Where(r => r.DoanKhoiID == 87 && r.NgayThanhLap >= tungay_dt && r.NgayThanhLap <= toingay_dt).ToList();
        var listgiaithecsd = _db.tblGiaiTheDonViDoans.Where(r => r.tblCoSoDoan.DoanKhoiID == 87 && r.NgayGiaiThe >= tungay_dt && r.NgayGiaiThe <= toingay_dt).ToList();
        int tlm_dcs = Get_count_by_creat_delete(listcosodoan, listgiaithecsd, 1, "DCS");
        int tlm_cdcs = Get_count_by_creat_delete(listcosodoan, listgiaithecsd, 1, "CDC");
        int tlm_cd = Get_count_by_creat_delete(listcosodoan, listgiaithecsd, 1, "CDD");
        int gt_dcs = Get_count_by_creat_delete(listcosodoan, listgiaithecsd, 0, "DCS");
        int gt_cdcs = Get_count_by_creat_delete(listcosodoan, listgiaithecsd, 0, "CDC");
        int gt_cd = Get_count_by_creat_delete(listcosodoan, listgiaithecsd, 0, "CDD");
        int tongdcs = tlm_dcs - gt_dcs;
        int tongcdcs = tlm_cdcs - gt_cdcs;
        int tongcd = tlm_cd - gt_cd;
        int tong = tongdcs + tongcdcs;
        string str = " <td>" + tong + "</td>"
            + "<td>" + tongdcs + "</td>"
            + "<td>" + tlm_dcs + "</td>"
            + "<td>" + gt_dcs + "</td>"
            + "<td>" + tongcdcs + "</td>"
            + "<td>" + tlm_cdcs + "</td>"
            + "<td>" + gt_cdcs + "</td>"
            + "<td>" + tongcd + "</td>"
            + "<td>" + tlm_cd + "</td>"
            + "<td>" + gt_cd + "</td>";
        lbltochucdoan.Text = str;
    }
    //co so doan giai the,thanh lap moi
    public int Get_count_by_creat_delete(List<tblCoSoDoan> listcsd,List<tblGiaiTheDonViDoan> listgiaithedv, int trangthai, string daidien)
    {

        int soluong = 0;
        if (trangthai == 1)//1 thanh lap moi
        {

            soluong = listcsd.Where(r => r.MaDonVi.Substring(0, 3).Trim() == daidien && r.DoanKhoiID == 87).Count();
        }
        else
            if (trangthai == 0)//giai the:nhung chua co
            {
                listgiaithedv.Where(r => r.MaDonVi.Substring(0, 3).Trim() == daidien).Count();
            }
        return soluong;
    }

}